#include "userstatus.h"
#include "ui_userstatus.h"
#include <QMenu>
#include <QLabel>
#include <QWidgetAction>
#include "blldataflow.h"
#include "globaldef.h"
#include "ettadatastruct.h"
#include "userlogger.h"

UserStatus::UserStatus(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserStatus)
{
    bdf = BllDataFlow::getInstance();
    //Signals and Slots

    connect(bdf, &BllDataFlow::setUserStatusTextSignal, this, &UserStatus::setUserStatusTextSlot);
    connect(bdf, &BllDataFlow::endExperimentSignal, this, &UserStatus::setEnableSlot);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &UserStatus::retranslateUiSlot);

    ui->setupUi(this);


    menu = new QMenu;
    QWidgetAction *actLogout = new QWidgetAction(this);
    logout = new QLabel(tr("Login out"));
    logout->setAlignment(Qt::AlignCenter);
    actLogout->setDefaultWidget(logout);
    menu->addAction(actLogout);
    menu->setFixedWidth(200);
    ui->btnUser->setMenu(menu);
    ui->btnUser->setToolButtonStyle(Qt::ToolButtonTextOnly);
    ui->btnUser->setPopupMode(QToolButton::InstantPopup);
    connect(actLogout, &QAction::triggered, this, &UserStatus::exitSlot);

    //Style
    QString menuStyleSheet = "QMenu{border:none;border-top:1px solid rgb(227,230,229);}QLabel{padding:15px 25px;font-size:16px;color:#333333;border-image:none;border:1px solid rgb(227,230,229);border-top:none;}"
            "QLabel:hover{background-color:rgba(223,246,252,1);}";
    menu->setStyleSheet(menuStyleSheet);
}

UserStatus::~UserStatus()
{
    delete ui;
}

void UserStatus::exitSlot()
{
    UserLogger::log(logout->text());
    bdf->changeMainPageIndex(MainPageIndex::LOGIN);
    bdf->logout();
}

void UserStatus::setUserStatusTextSlot(QString username)
{
    Q_UNUSED(username)
    ui->btnUser->setText(currentUsername);
}

void UserStatus::retranslateUiSlot()
{
    ui->retranslateUi(this);
    setUserStatusTextSlot(currentUsername);
    logout->setText(tr("Login out"));
}

void UserStatus::setEnableSlot(bool state)
{
    ui->btnUser->setEnabled(state);
}
